package app;

import app.database.MainDatabase;
import javax.swing.JDialog;

/**
 * Třída pro dialog, nabízející možnost připojení k databázi
 * @author Lukáš Marek
 */
public class ConnectionWindow extends JDialog {
	private MainDatabase db;
	boolean result = false;
	
	/**
	 * Vytvoří nový dialog
	 * @param database 
	 */
	public ConnectionWindow(MainDatabase database)
	{
		super((JDialog) null, true);
		db = database;
		initComponents();
		initButton.setVisible(false);
		this.setLocationRelativeTo(null);
	}
	
	/**
	 * Zobrazi dialog a vrazi vysledek po uzavreni
	 * @return result
	 */
	public boolean showDialog()
	{
		setVisible(true);
		return result;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents()
  {

    connectButton = new javax.swing.JButton();
    userField = new javax.swing.JTextField();
    passField = new javax.swing.JPasswordField();
    initButton = new javax.swing.JButton();
    MainLabel = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    addrField = new javax.swing.JTextField();
    portField = new javax.swing.JTextField();
    sidField = new javax.swing.JTextField();
    jLabel7 = new javax.swing.JLabel();
    jLabel8 = new javax.swing.JLabel();
    jLabel9 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Nastavení připojení");
    setForeground(java.awt.Color.white);
    setMinimumSize(new java.awt.Dimension(615, 345));
    setResizable(false);
    getContentPane().setLayout(null);

    connectButton.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    connectButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/symbol_connect2.png"))); // NOI18N
    connectButton.setText("Připojit");
    connectButton.setToolTipText("připojí se k databázi");
    connectButton.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        connectButtonActionPerformed(evt);
      }
    });
    getContentPane().add(connectButton);
    connectButton.setBounds(447, 220, 140, 33);

    userField.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    getContentPane().add(userField);
    userField.setBounds(390, 70, 200, 22);

    passField.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    getContentPane().add(passField);
    passField.setBounds(390, 100, 200, 22);

    initButton.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    initButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/symbol_start.png"))); // NOI18N
    initButton.setText("Nahrát startovací data");
    initButton.setToolTipText("v připojené databázi vytvoří všechny tabulky a inicializuje je vzorovými daty");
    initButton.setEnabled(false);
    getContentPane().add(initButton);
    initButton.setBounds(10, 220, 203, 33);

    MainLabel.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
    MainLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/symbol_database_basic.png"))); // NOI18N
    MainLabel.setText("Přihlášení k databázi");
    getContentPane().add(MainLabel);
    MainLabel.setBounds(195, 11, 260, 24);

    jLabel4.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    jLabel4.setText("Uživatelské jméno:");
    getContentPane().add(jLabel4);
    jLabel4.setBounds(10, 70, 120, 16);

    jLabel5.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    jLabel5.setText("Heslo:");
    getContentPane().add(jLabel5);
    jLabel5.setBounds(10, 100, 40, 16);

    addrField.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    getContentPane().add(addrField);
    addrField.setBounds(10, 170, 324, 22);

    portField.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    getContentPane().add(portField);
    portField.setBounds(340, 170, 82, 22);

    sidField.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    getContentPane().add(sidField);
    sidField.setBounds(430, 170, 158, 22);

    jLabel7.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    jLabel7.setText("Adresa databáze:");
    getContentPane().add(jLabel7);
    jLabel7.setBounds(10, 150, 113, 16);

    jLabel8.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    jLabel8.setText("Port:");
    getContentPane().add(jLabel8);
    jLabel8.setBounds(340, 150, 31, 16);

    jLabel9.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    jLabel9.setText("SID:");
    getContentPane().add(jLabel9);
    jLabel9.setBounds(430, 150, 27, 16);

    pack();
  }// </editor-fold>//GEN-END:initComponents

	/**
	 * pripojeni k databazi
	 * @param evt 
	 */
  private void connectButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_connectButtonActionPerformed
  {//GEN-HEADEREND:event_connectButtonActionPerformed
		char[] pw = passField.getPassword();
		String pws = new String(pw);
		int port;
		try
		{
			port = Integer.parseInt(portField.getText());
		}
		catch ( NumberFormatException exc )
		{
			port = 0;
		}
		result = db.Login(userField.getText(), pws, addrField.getText(), port, sidField.getText());
		
		if(result)
		{
			setVisible(false);
			dispose();
		}
		else
		{
			MainLabel.setText("Přihlášení k databázi.. CHYBA");
		}
  }//GEN-LAST:event_connectButtonActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel MainLabel;
  private javax.swing.JTextField addrField;
  private javax.swing.JButton connectButton;
  private javax.swing.JButton initButton;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel7;
  private javax.swing.JLabel jLabel8;
  private javax.swing.JLabel jLabel9;
  private javax.swing.JPasswordField passField;
  private javax.swing.JTextField portField;
  private javax.swing.JTextField sidField;
  private javax.swing.JTextField userField;
  // End of variables declaration//GEN-END:variables
}
